    package com.example.share.controller;


import com.example.share.mapper.GoodMapper;
import com.example.share.pojo.Category;
import com.example.share.pojo.CategoryGood;
import com.example.share.pojo.Good;
import com.example.share.pojo.User;
import com.example.share.service.SubmitService;

import com.example.share.util.CommonResult;
import com.example.share.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author haoren
 */
@RestController
public class SubmitController {
    @Autowired
    private SubmitService submitService;
    //添加物品
    @RequestMapping("/summ")
    //public CommonResult<Integer> addGood(  @RequestParam("goodname") String goodname,@RequestParam("number")  int number,@RequestParam("addtime")  Date addtime ,@RequestParam("categroy")  String category ){
    //public String addGood(  @RequestParam("goodname") String goodname,@RequestParam("number")  int number,@RequestParam("addtime")  Date addtime ,@RequestParam("categroy")  String category ){
    //public String addGood(){
    public CommonResult<Integer> addGood(){
        //前端传回数据包含
        String goodname="tiaafsdasfdo";
        int number= 3;
        Date addtime = new Date();
        String category="5fdssadfsda5";
        int userId=2;



        Integer SUM=null;
        int i = 0;

        System.out.println(goodname+number+addtime+category);

        Category category1 = submitService.findCategory(category);

        //判断分类是否存在，如果没有则添加
        if( category1==null){
            submitService.addCategory(category);
            category1 = submitService.findCategory(category);
        }

        Good good=submitService.findGood(goodname);
        //判断物品是否存在，若果没就添加，有就修改
        if (good==null){
            good = new Good(goodname,number ,addtime);
            SUM =submitService.addGood(good);

        }else {
            good.setGoodNumber(good.getGoodNumber()+number);

            SUM =submitService.reviseGood(good);
        }

        CategoryGood categoryGood= submitService.findCategoryGood(good.getGoodId());
        //判断是否存在中间表，不存在就添加
        if(categoryGood==null){
            categoryGood=new CategoryGood(null,category1.getCategoryId(),good.getGoodId(),1);
            Integer integer = submitService.addCategoryGood(categoryGood);
            System.out.println(integer+"fghdasfjgjkhghashfksghgfjsdgskgsjdkfshjhhhhhhhh");
        }
        //用户添加贡献

        User user = submitService.finduser(userId);
        user.setUserContributionDegree(user.getUserContributionDegree()+1);
        int j = submitService.reviseUser(user);
    // return "kfajsdkjsdf";
    System.out.println(j);
        //return "seclet";
 //返回结果
        if (SUM==1&&j==1){
            return new CommonResult(ResultCode.SUCCESS,true);
        }else {
           return new CommonResult(ResultCode.SUCCESS,false);
        }

    }

}
